#!/bin/bash
# - qcow2_os
# - qcow2_arch
# - qcow2_version
# - qcow2_url
# - rootfs_dir
# - rootfs_protocol
# - rootfs_server
# - rootfs_path

umask 0002

. $LKP_SRC/lib/env.sh
. $LKP_SRC/lib/log.sh

check_yaml_vars()
{
	log_info "starting check yaml vars ..."

	local yaml_vars=(
		"qcow2_os"
		"qcow2_arch"
		"qcow2_version"
		"rootfs_protocol"
		"rootfs_server"
		"rootfs_path"
	)

	for yaml_t in "${yaml_vars[@]}"
	do
		[ -n "$(eval echo "\$${yaml_t}")" ] || die "cannot find value of var: ${yaml_t}."
	done
}

set_qcow2rootfs_tools()
{
        export QCOW2_PATH="/c/compass-ci/container/qcow2rootfs"
        export CCI_SRC="/c/compass-ci"
        [ -d "$CCI_SRC" ] || {
                git clone https://gitee.com/wu_fengguang/compass-ci.git /c/compass-ci
        }
        rm ${QCOW2_PATH}/root/$(arch)/etc/yum.repos.d/*
	wget -O ${QCOW2_PATH}/root/$(arch)/etc/yum.repos.d/CentOS-Base.repo https://repo.oepkgs.net/openEuler/compass-ci/cci-deps/repo/$(arch)/CentOS-Base.repo

        mkdir -p /etc/systemd/system/docker.service.d
        cat >/etc/systemd/system/docker.service.d/10-ramdisk.conf << EOF
        [Service]
        Environment=DOCKER_RAMDISK=true
EOF

        systemctl daemon-reload
        systemctl restart docker

        cd ${QCOW2_PATH}
        ./build || {
                echo "build container failed~"
                exit 1
        }
        sed -i 's#$EXTRACT_ROOT/bin/extract $QCOW2_FILE $tar_out_file#$EXTRACT_ROOT/bin/extract $EXTRACT_ROOT/qcow2-dir/$QCOW2_NAME $tar_out_file#' \
                ${QCOW2_PATH}/bin/main

}

check_url()
{
	export QCOW2_NAME="${qcow2_os}-${qcow2_version}-${qcow2_arch}.qcow2.xz"

	# Since the 21.03 version of OpenEuler's mirror naming format has changed,
	# it has been modified.
	if [ "${qcow2_version}" == "8.6" ];then
	       export QCOW2_NAME="${qcow2_os}-${qcow2_version}-${qcow2_arch}.qcow2"
	fi

	[ -n "$qcow2_url" ] && {
		export QCOW2_URL="$qcow2_url"
	} || {
		die "The URL of the qcow2 file is not given."
	}
}

download_qcow2_img()
{
	mkdir -p ${IMG_PATH} && cd ${IMG_PATH}
	[ -f "${IMG_PATH}/${QCOW2_NAME}" ] || {
		wget -O $QCOW2_NAME ${QCOW2_URL} || exit 1
	}
}

mount_os_rw()
{
	export IMG_PATH="/root/qcow2"
	export MOUNT_POINT="${IMG_PATH}/mnt"
	mkdir -p ${MOUNT_POINT}
	mount -t ${rootfs_protocol} -o rw ${rootfs_server}:/${rootfs_path} ${MOUNT_POINT}

	export ROOTFS_DIR="$rootfs_dir"
	export ROOTFS_PATH="${MOUNT_POINT}/openeuler/${qcow2_arch}/${qcow2_version}-${ROOTFS_DIR}"
	[ -d "$ROOTFS_PATH" ] && {
		if [ "`ls -A $ROOTFS_PATH`" = "" ];then
			echo "$ROOTFS_PATH is indeed empty"
		else
			umount ${MOUNT_POINT}
			echo $ROOTFS_PATH
			exit 0
		fi
	}
}

extract_rootfs()
{
	mkdir -p $ROOTFS_PATH
	wget -O debian:dracut.tar https://repo.oepkgs.net/openEuler/compass-ci/cci-deps/docker/$(arch)/debian:dracut.tar
        docker load -i debian:dracut.tar

	cd ${QCOW2_PATH}
	./run ${IMG_PATH}/${QCOW2_NAME} ${ROOTFS_PATH}
}

set_resolv_config()
{
	cat >${ROOTFS_PATH}/etc/resolv.conf<<-EOF
	nameserver 114.114.114.114
	EOF
}

set_link()
{
	cd ${MOUNT_POINT}/openeuler/${qcow2_arch}
	if [ "${ROOTFS_DIR##rc*}" != "${ROOTFS_DIR}" ]; then
		rc_version=${ROOTFS_DIR%%_openeuler*}
		ln -fsT ${qcow2_version}-${ROOTFS_DIR} ${qcow2_version}-$rc_version-iso
		ln -fsT ${qcow2_version}-${ROOTFS_DIR} ${qcow2_version}-$rc_version
	elif [ "${ROOTFS_DIR##test*}" != "${ROOTFS_DIR}" ]; then
		rc_version=${ROOTFS_DIR%%_openeuler*}
		ln -fsT ${qcow2_version}-${ROOTFS_DIR} ${qcow2_version}-$rc_version-iso
		ln -fsT ${qcow2_version}-${ROOTFS_DIR} ${qcow2_version}-$rc_version
	else
		ln -fsT ${qcow2_version}-${ROOTFS_DIR} ${qcow2_version}-dailybuild-iso
		ln -fsT ${qcow2_version}-${ROOTFS_DIR} ${qcow2_version}-dailybuild
	fi
	umount ${MOUNT_POINT}
	echo $ROOTFS_PATH
}

mount_os_rw
check_yaml_vars
set_qcow2rootfs_tools
check_url
download_qcow2_img
extract_rootfs
set_resolv_config
set_link
